<?php
###############   COPYLEFT GPLv3 LICENSE   ###############
##
## JFX Version 0.2.7
## Website Management Software
## www.jfxcms.com
##
## Copyright 2009 GPLv3 - http://www.opensource.org/licenses/gpl-3.0.html
##
## Anthony Gallon
## oi_antz@hotmail.com
##
## Permission is hereby granted to any person having a copy of this software
## to freely use and modify as required so long as the copyright notices
## and branding remain intact.
##
## Full license details available at http://www.jfxcms.com/license
##
###############   COPYLEFT GPLv3 LICENSE   ###############

// this file should only be called once per page request

if(defined('JFX_SYSTEM_START')) return;
define('JFX_SYSTEM_START', '1');


global $JFXMemDebug, $JFXDBDebug;


$dirname = rtrim(str_replace(DIRECTORY_SEPARATOR, '/', dirname(__FILE__)), '/');

// for testing, ignore this
function getmicrotime()
{
  list($usec, $sec) = explode(" ",microtime());
   return ((float)$usec + (float)$sec);
}
$pageLoadStartTime = getmicrotime();
$pageLoadLog = 'Starts at '.$pageLoadStartTime."\n";
// end for testing


$JFXMemDebug[] = array('bytes'=>memory_get_usage(), 'file'=>__FILE__, 'line'=>__LINE__, 'time' => getmicrotime(), 'desc' => 'System-start has just been called');





if(file_exists($dirname.'/jfx-config.php')){
    require_once('jfx-config.php');
}else{
    // no config exists, lets run installer
    require('jfx-install.php');
    exit;
}


// get our path configurations
include($CONFIG->classesDir.'/JFX/Constants.php');
require_once($CONFIG->classesDir.'/JFX/Functions.php');
JFX::register('config', $CONFIG);
JFX::register('JFXConfig', $CONFIG);


$JFXMemDebug[] = array('bytes'=>memory_get_usage(), 'file'=>__FILE__, 'line'=>__LINE__, 'time' => getmicrotime(), 'desc' => 'Constants, functions and config set');


/**
 *  Create a default database object
 */
$DB_REGISTRY = new JFX_Object;
JFX::register('db_registry', $DB_REGISTRY);
$DB = JFX::getDb('site');

/**
 *  Work out the correct domain and urls
 */
$CONFIG->domain = $_SERVER['SERVER_NAME'];

if($DB->countRows($CONFIG->dbprefix.'domains', "domain_name = '{$CONFIG->domain}' AND is_active = 1")==0){
    // uh-oh, cannot serve this domain
    $newDomain = (string) $DB->oneValue($CONFIG->dbprefix.'domains', 'domain_name', "is_active = 1 AND is_default = 1");
    if($newDomain == ''){
        $toFrom = array(
            'toName' => 'SITE ADMINISTRATOR',
            'toEmail' => $CONFIG->adminEmail,
            'fromName' => 'SITE NOTIFIER',
            'fromEmail' => $CONFIG->adminEmail
        );
        JFX_Mail::sendRaw('The site is mis-configured', '
A request was made to '.$CONFIG->domain.' but this domain is not active. Furthermore, I was unable to find a default domain. Please
configure a default domain in the control panel: <a href="http://'.$CONFIG->domain.$CONFIG->adminFolder.'">'.'http://'.$CONFIG->domain.$CONFIG->adminFolder.'</a>'
, $toFrom);
    exit('Error: could not find the requested page');
    }

    // using the default domain
    $CONFIG->domain = $newDomain;
}else{
    $CONFIG->domainid = $DB->oneValue($CONFIG->dbprefix.'domains', 'id', "domain_name = '{$CONFIG->domain}'");
}



// set up our urls now that we have a valid domain
$CONFIG->baseUrl = 'http://'.$CONFIG->domain.$CONFIG->prefix;

$CONFIG->adminUrl = 'http://'.$CONFIG->domain.$CONFIG->prefix.$CONFIG->adminFolder;
$CONFIG->jsUrl = $CONFIG->baseUrl.'/jfx/js';
$CONFIG->cssUrl = $CONFIG->baseUrl.'/jfx/css';
$CONFIG->imageUrl = $CONFIG->baseUrl.'/jfx/images/'.$CONFIG->theme;
$safeDomain = $DB->escape($CONFIG->domain);
$CONFIG->domainid = $DB->oneValue($CONFIG->dbprefix.'domains', 'id', "domain_name = '{$safeDomain}'");
if(empty($_SERVER['HTTPS'])){
    $protocol = 'http://';
    $CONFIG->protocol = 'HTTP';
}else{
    $protocol = 'https://';
    $CONFIG->protocol = 'HTTPS';
}
define('THIS_PAGE_URL', $protocol.$CONFIG->domain.'/'.ltrim($_SERVER['REQUEST_URI'], '/'));

$JFXMemDebug[] = array('bytes'=>memory_get_usage(), 'file'=>__FILE__, 'line'=>__LINE__, 'time' => getmicrotime(), 'desc' => 'Domain detected and applied to config');


/**
 * Include phpQuery classes
 */
if(!class_exists('phpQuery')){
    require_once($CONFIG->classesDir.'/PHPQuery/phpQuery.php');
}

/**
 *  Obtain a session object for the user, and register to registry
 */
$USER = JFX_User::getInstance();
JFX::register('JFX_User', $USER);


$DB->debug(($CONFIG->debug && $USER->isAdmin()));
$DB->showErrors(($CONFIG->debug && $USER->isAdmin()));


$JFXMemDebug[] = array('bytes'=>memory_get_usage(), 'file'=>__FILE__, 'line'=>__LINE__, 'time' => getmicrotime(), 'desc' => 'User object instantiated');



/**
 *  Prevent pesky "Are you sure you want to resubmit this form data?" alerts
 */
Antz_IntelliForm::antiRepost();
//Antz_IntelliForm::purge();



/**
 *  Create the smarty template engine for registering page variables
 */
require_once($CONFIG->classesDir.'/Smarty/Smarty.class.php');
$SMARTY = new JFX_Smarty;
JFX::register('Smarty', $SMARTY);

$JFXMemDebug[] = array('bytes'=>memory_get_usage(), 'file'=>__FILE__, 'line'=>__LINE__, 'time' => getmicrotime(), 'desc' => 'Smarty object instantiated');



/**
 *  Create the page object, which is used for adding elements to the page
 */
$VIEW = new JFX_View();
JFX::register('JFX_View', $VIEW);


$JFXMemDebug[] = array('bytes'=>memory_get_usage(), 'file'=>__FILE__, 'line'=>__LINE__, 'time' => getmicrotime(), 'desc' => 'Page object instantiated');


/**
 *  Create the encryption object, which is used for encrypting and decrypting sensitive data
 */
$CRYPT = new JFX_Crypt($CONFIG->cryptSalt);
JFX::register('JFX_Crypt', $CRYPT);

$JFXMemDebug[] = array('bytes'=>memory_get_usage(), 'file'=>__FILE__, 'line'=>__LINE__, 'time' => getmicrotime(), 'desc' => 'Crypt object instantiated');

/**
 * Lets get our lang object
 */
$LANG = JFX_Lang::getInstance();
JFX::register('JFX_Lang', $LANG);
JFX::register('lang', $LANG);


$JFXMemDebug[] = array('bytes'=>memory_get_usage(), 'file'=>__FILE__, 'line'=>__LINE__, 'time' => getmicrotime(), 'desc' => 'Language object instantiated');



/**
 * Archive object
 */
error_reporting(E_ALL);
require_once($CONFIG->classesDir.'/ThirdParty/Archive.php');

/**
 * Settings object
 */
$SETTINGS = new JFX_Settings;
$SETTINGS->load();
JFX::register('JFX_Settings', $SETTINGS);


// load the modules
$installedModules = JFX_Module::getInstalledModules();
foreach($installedModules as $module){
    $moduleObject = JFX::registry('JFX_Module_'.ucfirst($module['keyname']));
    $JFXMemDebug[] = array('bytes'=>memory_get_usage(), 'file'=>__FILE__, 'line'=>__LINE__, 'time' => getmicrotime(), 'desc' => 'Loaded Module: '.$module['title']);

}

$INPUT = new JFX_Input;
JFX::register('JFX_Input', $INPUT);

// link module
$linkModule = $SETTINGS->get('module-link');
if(JFX_Module::isValidModule($linkModule)) $LINK = JFX_Module::newInstance($linkModule);
else JFX::addError('Link Module not defined in '.$CONFIG->dbprefix.'settings WHERE k = "module-link"');
JFX::register('JFX_Link', $LINK);

$JFXMemDebug[] = array('bytes'=>memory_get_usage(), 'file'=>__FILE__, 'line'=>__LINE__, 'time' => getmicrotime(), 'desc' => 'Link object instantiated');

// theme
$theme = $SETTINGS->get('theme-admin');

if(JFX_Theme::isValidTheme($theme)) $THEME = JFX_Theme::newInstance($theme);
else JFX::addError('Theme not defined in '.$CONFIG->dbprefix.'settings WHERE k = "theme"');
JFX::register('JFX_Theme', $THEME);
JFX::register('JFX_Theme_'.ucfirst(strtolower($theme)), $THEME);

$JFXMemDebug[] = array('bytes'=>memory_get_usage(), 'file'=>__FILE__, 'line'=>__LINE__, 'time' => getmicrotime(), 'desc' => 'Theme loaded');

// core module
$coreModule = $SETTINGS->get('module-core');
if(JFX_Module::isValidModule($coreModule)) $CORE = JFX_Module::newInstance($coreModule);
else JFX::addError('Core Module not defined in '.$CONFIG->dbprefix.'settings WHERE k = "module-core"');
JFX::register('JFX_Core', $CORE);
JFX::register('JFX_Module_'.ucfirst(strtolower($coreModule)), $CORE);


// extra classes: geshi
//require_once($CONFIG->classesDir.'/geshi_dev/class.geshi.php');
require_once($CONFIG->classesDir.'/geshi/geshi.php');

$SMARTY->assign('coreTitle', $CORE->getTitle());
$SMARTY->assign('coreVersion', $CORE->getVersion());
$SMARTY->assign('themeTitle', $THEME->getTitle());
$SMARTY->assign('themeVersion', $THEME->getVersion());





// use this to clear the cache, todo: make a button for this :D
//$MM = JFX::registry('JFX_Module_Manager');
//$MM->rebuildEventCache();
//echo 'done';


ob_start();
// please continue with your own script then include jfx-system-end
























